package gui.editor;

import generated.domain.avatar.Avatar;
import generated.domain.room.Room;
import generated.domain.world.World;
import gui.perspective.ChatRoomPerspective;
import gui.view.GlobalChatView;
import gui.view.RoomView;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.part.EditorPart;

import vworld.Client;

public class WorldMapEditor extends EditorPart {
	public static final String ID = "gui.editor.WorldMapEditor";
	private int NUMBER_OF_COLUMN = 0;
	private GridLayout layout = new GridLayout();
	private GridData data;
	private Button square;
	private Display display;

	public WorldMapEditor() {
	}

	private void createSquares(Composite parent) {

		World world = Client.CURRENT_WORLD;
		for (final Room room : world.rooms) {
			data = new GridData(GridData.FILL_BOTH);
			square = new Button(parent, SWT.CENTER);
			square.setBounds(parent.getClientArea());
			square.setText(room.name);
			square.setToolTipText("Row " + room.location.x + " and Column "
					+ room.location.y);
			square.setLayoutData(data);
			square.addSelectionListener(new SelectionListener() {
				public void widgetDefaultSelected(SelectionEvent e) {
				}

				public void widgetSelected(SelectionEvent event) {
					try {
						IWorkbenchPage page = PlatformUI.getWorkbench()
								.getActiveWorkbenchWindow().getActivePage();
						page.closePerspective(page.getPerspective(), false,
								false);
						PlatformUI.getWorkbench().showPerspective(
								ChatRoomPerspective.ID,
								PlatformUI.getWorkbench()
										.getActiveWorkbenchWindow());
					} catch (WorkbenchException e) {
						e.printStackTrace();
					} finally {
						getEditorSite().getPage().closeEditor(
								WorldMapEditor.this, false);
					}

					Avatar userAvatar = Client.USER_SERVICE
							.getAvatar(Client.CONNECTED_USER);
					Client.CALL_BACK_REF.roomName(room.name);
					Client.CONNECTED_USER.avatar.state = true;
					Client.CURRENT_ROOM = Client.ROOM_SERVICE.joinRoom(
							Client.CALL_BACK_REF, userAvatar, room.name);

					changeGlobalChatViewTitle(room.name);
					changeRoomViewTitle(room.name);
				}
			});
		}
	}

	private void changeGlobalChatViewTitle(String roomName) {
		IWorkbenchWindow window = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow();
		IWorkbenchPage page = window.getActivePage();
		IViewPart globalChatView = page.findViewReference(GlobalChatView.ID)
				.getView(false);

		if (globalChatView != null) {
			((GlobalChatView) globalChatView).changeTitle(roomName + " "
					+ globalChatView.getTitle());
		}
	}

	private void changeRoomViewTitle(String roomName) {
		IWorkbenchWindow window = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow();
		IWorkbenchPage page = window.getActivePage();
		IViewPart roomView = page.findViewReference(RoomView.ID).getView(false);

		if (roomView != null) {
			((RoomView) roomView).changeTitle(roomName);
		}
	}

	@Override
	public void createPartControl(Composite parent) {
		display = parent.getDisplay();
		NUMBER_OF_COLUMN = Client.CURRENT_WORLD.size.columnNumber;
		layout.numColumns = NUMBER_OF_COLUMN;
		layout.makeColumnsEqualWidth = true;
		int squareSize = 0;
		int width = display.getBounds().width;
		int height = display.getBounds().height;
		if (width >= height) {
			squareSize = height / 2;
		} else {
			squareSize = width / 2;
		}
		parent.setSize(squareSize, squareSize);
		parent.setBounds(new Rectangle(squareSize / 4, squareSize / 4,
				squareSize, squareSize));
		parent.setLayout(layout);

		createSquares(parent);
	}

	@Override
	public void setFocus() {
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}
}
